package com.szyjjk.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szyjjk.entity.PageBo;
import com.szyjjk.entity.ResponseBo;
import com.szyjjk.entity.UserBo;
import com.szyjjk.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	
	/**
	 * 分页查询用户
	 * @param request
	 * @param userBo
	 * @return
	 */
	@RequestMapping(value = "/queryList.do")
	@ResponseBody
	public PageBo<UserBo> queryList(HttpServletRequest request,UserBo userBo,String pageNo) {
//		String pageNo = reque	st.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		Map<String,Object> param = new HashMap<>();
		
		if(StringUtils.isBlank(pageSize)) {
			param.put("pageSize", 10);
		}else {
			param.put("pageSize", Integer.parseInt(pageSize));
		}
		if(StringUtils.isBlank(pageNo)) {
			param.put("pageNo", 1);
		}else {
			param.put("pageNo", Integer.parseInt(pageNo));
		}
		int p = (Integer.parseInt(param.get("pageNo")+"")-1) *Integer.parseInt(param.get("pageSize")+"");

		param.put("pageStart", p);
		if(StringUtils.isNotBlank(userBo.getUserName())) {
			param.put("userName", "%"+userBo.getUserName()+"%");
		}
		PageBo<UserBo> page = userService.queryListByPage(param);
		return  page;
	}
	/**
	  * 新增或修改
	 * @param request
	 * @param userBo
	 * @return
	 */
	@RequestMapping(value = "/addUser.do")
	@ResponseBody
	public ResponseBo addUser(HttpServletRequest request,UserBo userBo) {
		// 参数校验
		System.out.println(userBo.getUserName());
		userBo.setCreateUserId("admin");
		userBo.setCreateTime(new Date());
		userBo.setUpdateUserId("admin");
		userBo.setUpdateTime(new Date());
//		UserBo u =new UserBo();
//		u.setId(112);
//		u.setCreateTime(new Date());
//		u.setCreateUserId("admin");
//		u.setDeleteFlag(1);
//		u.setEmail("23134646@qq.com");
//		u.setNickName("小刘");
//		u.setPassWord("123456");
//		u.setPhone("18712341234");
//		u.setUpdateTime(new Date());
//		u.setUpdateUserId("admin");
//		u.setUserName("xiaoliu");
//		u.setUserState(1);
//		u.setSex("男");
 	return userService.addUser(userBo);
	}





	@RequestMapping(value = "/delUser.do")
	@ResponseBody
	public ResponseBo delUser(HttpServletRequest request,UserBo userBo) {
		ResponseBo rb = new ResponseBo();
		if(userBo.getId()==null) {
			rb.setCode(0);
			rb.setMessage("id为空");
			return rb;
		}
		return userService.delUser(userBo);
	}
}
